package com.whately.core.base.sms;

import com.whately.core.base.constant.DebugSwitcher;
import com.whately.core.base.helper.PubThreadFactory;
import com.whately.core.base.utils.MobileUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class SmsFacade {

    @Autowired
    private DebugSwitcher debugSwitcher;

    @Value("${qcloud.sms.sendSmsFlagInOffLineProfile:false}")
    private boolean sendSmsFlagInOffLineProfile;

    @Autowired
    private QcloudSmsInfo qcloudSmsInfo;

    @Autowired
    private WhRlzxSmsInfo whRlzxSmsInfo;


    //线程池
    private ExecutorService executorService = new ThreadPoolExecutor(2, 10, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1000), new PubThreadFactory("sms-service-processor"));



    public void sendSms( String mobile, String content) {
        executorService.execute(() -> {
            sendSmsSync(  mobile,  content);
        });
    }


    public SmsSendResult sendSmsSync( String mobile, String content) {
        SmsSendResult result = new SmsSendResult();
        if(StringUtils.isNotBlank(content)&& MobileUtils.isPhone(mobile)){
            //发送短信，并记录发送结果
            try{
                log.info("sendMsgCaller sendMsgCaller  , mobile {} ,content {} ",mobile,content);
                return SendSmsUtil.sendSingleSms(qcloudSmsInfo,mobile,content);
            }catch (Exception e){
                log.error("call sms sender error!",e);
                return result;
            }

        }
        return result;
    }




    /**
     *测试一信通平台
     * 威海人社局
     */

    public void sendWhrlzxSms(String mobile, String content) {
        executorService.execute(() -> {
            sendWhrlzxSmsSync( mobile,  content);
        });
    }

    public SmsSendResult sendWhrlzxSmsSync(String mobile, String content) {
        SmsSendResult result = new SmsSendResult();
        if(StringUtils.isNotBlank(content)&& MobileUtils.isPhone(mobile)){

            boolean localOrDev = debugSwitcher.isProfileLocalOrDev();
            try{
                log.info("sendMsgCaller sendMsgCaller , mobile {} ,content {} ",mobile,content);

                if(localOrDev&&!sendSmsFlagInOffLineProfile){
                    result.setExecuteResult(true);
                    result.setSendStatus(false);
                    result.setDescription("测试环境未发送");
                    log.info("localOrDev {},sendSmsFlagInOffLineProfile {},so not send sms and return finish here ",localOrDev,sendSmsFlagInOffLineProfile);
                    return result;
                }

                return SendSmsUtil.sendWhrlzxSms(whRlzxSmsInfo,mobile,content);

            }catch (Exception e){
                log.error("call whrlzxSms sender error!",e);
                return result;
            }
        }
        return result;
    }


}
